/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MyStatementExecution.MyDelete;

import MyCondition.AndOrHandler;
import MyCondition.ConditionHandler;
import MyDataContainer.MyQuery;
import MyStatementExecution.MyStatement;
import MyStatementExecution.MyStatementKind;
import MyDataContainer.MyTable;
import MyStatementExecution.MySelect.MySelectStatement;
import StringJoinerCutter.*;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Welcome
 */
public class MyDeleteStatement extends MyStatement{

    //private MyTable fromTable;
    private AndOrHandler whereAndOrHandler;
    
    public MyDeleteStatement() {
        myStatementKind = MyStatementKind.DELETE;
        whereAndOrHandler=new AndOrHandler();
    }
    

    
    @Override
    public MyStatementKind getKind() {
        return this.myStatementKind;
    }

    @Override
    public void executeStatement() {
        try {
            //Table delete data
           String tableName = StringJoinerCutter.getStringInMidle(statementString, "from", "where").trim();
           MyTable targetTable = databaseHandler.getTable(tableName);
           
           String whereString = StringJoinerCutter.getStringInMidle(statementString, "where", "").trim();
           if(whereString.equals(""))
           {
               targetTable.clearAll();
               return;
           }
           
           String newTempSelect = "select * from "+StringJoinerCutter.getStringInMidle(statementString, "from", "");
           MySelectStatement selectStatement = new MySelectStatement(databaseHandler, newTempSelect);
           
           MyQuery tempQuery = new MyQuery(selectStatement.executeSelectStatement());
           
           for(int i=0; i<targetTable.getSize();i++)
           {
               for(int j=0; j<tempQuery.getSize();j++)
                   if(targetTable.getRowAt(i).isEqual(tempQuery.getRowAt(j)))
                   {
                       targetTable.removeAt(i);
                   }
           }
           
           databaseHandler.WriteDatabase(databaseHandler.getPathFile());
           targetTable.showData();
           
        } catch (Exception ex) {
            Logger.getLogger(MyDeleteStatement.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void setWhereCondition(String tempString) {
        whereAndOrHandler.setConditionString(tempString);
        whereAndOrHandler.splitConditions();
    }
    
}
